<template>
	<view class="container">
		<view class="form">
			<view> <text>商品ID:</text> <input  type="text" v-model="obj.goodsID" /> </view>
			<view> <text>商品名称:</text><input  type="text" v-model="obj.spmc" /> </view>
			<!-- <view> <text>商品信息编号:</text><input  type="text" v-model="obj.spxxbh" /> </view> -->
			<view> <text>商品单价:</text><input type="text" v-model="obj.spdj"/> </view>
			<view> <text>商品折扣:</text><input type="text" v-model="obj.spzk"/> </view>
			<view> <text>商品图片路径:</text><input  type="text" v-model="obj.sptpcflj"/> </view>
			<view> <text>商品分类:</text>{{type}}</view>
			<view class="special">
				<button type="primary" size="mini" @click="confirm">确认添加</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {BASE_URL} from '../../api/ajax.js'
	export default{
		data(){
			return{
				obj:{
					goodsID:'',
					spmc:'',
					// spxxbh:'',
					spdj:'',
					spzk:'',
					sptpcflj:'',
				},
				arr:[{i:1,type:'水果'},{i:2,type:'零食'}]
			}
		},
		
		methods:{
			confirm(){
				console.log('confirm',this.obj)
				
				uni.request({
					url: BASE_URL + '/addgoods',
					// url:'http://47.95.216.146:8080/supermarket/addgoods',
					method:'POST',
					data:this.obj,
					success:()=>{
						console.log('添加成功')
						this.$store.dispatch('get_goods')
						uni.showToast({
							title: '添加成功'
						})
					},
					fail: () => {
						uni.showToast({
							title: '添加失败'
						})
					}
				})
			}
			
		},
		
		computed:{
			type(){
				let flag = this.obj.goodsID.slice(0,1)
				let want = this.arr.find((element)=>{
					return flag == element.i
				})
				return want?want.type:''
			}
		}
	}
</script>

<style lang="scss">
	.container{
		position:relative ;
	}
	.form{
		// border: 1rpx solid #f00;
		padding: 10rpx;
		// display: flex;
		// flex-direction: column;
		// align-items: center;
		view{
			// border: 1rpx solid #00f;
			margin-bottom: 50rpx;
			
		}
		text{
			font-weight: bold;
			font-size: 33rpx;
		}
		input{
			border-bottom: 1rpx solid #b2b2b2;
			// border: 1rpx solid #f00;
			display: inline-block;
			vertical-align: middle;
			// text-align: cener;
		}
		.special{
			text-align: center;
		}
	}
</style>
